//
//  RHDeviceInfoListVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/5.
//

#import "RHDeviceInfoListVC.h"
#import "RHDeviceInfoListCell.h"
#import "RHDeviceInfoBaseVC.h"
#import "SYChartTool.h"
#import "RHDevInfoModel.h"
#import "RHDeviceSecretVC.h"

@interface RHDeviceInfoListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray *infoArr;

@end

@implementation RHDeviceInfoListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"详情".rh_localized;
    [self setupUI];
}

- (void)setupUI{
    kWeakSelf;
    NSMutableArray *tempArr = [NSMutableArray array];
    //最后一个行加个设备密钥
    
//    NSArray fileArr = @[@"device_station_detail",@"device_battery_detail",@"device_software_detail",@"device_hardware_detail",@""];
    NSArray *fileArr = @[@"device_station_detail",@"device_system_detail",@""];//device_system_detail
    [fileArr enumerateObjectsUsingBlock:^(NSString*  _Nonnull jsonFile, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dic = [SYChartTool readLocalJsonFileWithName:jsonFile];
        if(idx == fileArr.count - 1){
            RHDevInfoModel *secretModel = [[RHDevInfoModel alloc]init];
            secretModel.sectionTitle = @"设备密钥";
            secretModel.icon = @"device_detail_secret";
            secretModel.typeIcon = @"device_detail_secret";
            [tempArr addObject:secretModel];
            
        }else{
            RHDevInfoModel *infoModel = [RHDevInfoModel yy_modelWithJSON:dic];
            [tempArr addObject:infoModel];
        }

    }];
    
    self.infoArr = tempArr.copy;
   
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = CFTabViewBgColor;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHDeviceInfoListCell class] forCellReuseIdentifier:@"RHDeviceInfoListCell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.infoArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section == 0){
        UIView *headView = kView;
        headView.backgroundColor = CFTabViewBgColor;
        return headView;
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0)return 10;
    return 0.0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    RHDeviceInfoListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHDeviceInfoListCell"];
    RHDevInfoModel *infoModel = self.infoArr[indexPath.section];
    cell.infoModel = infoModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == self.infoArr.count-1) {
        RHDeviceSecretVC *secretVC = [[RHDeviceSecretVC alloc]init];
        secretVC.deviceId = self.deviceModel.deviceId;
        [self.navigationController pushViewController:secretVC animated:YES];
    }else{
        RHDevInfoModel *infoModel = self.infoArr[indexPath.section];
        RHDeviceInfoBaseVC *infoVC = [[RHDeviceInfoBaseVC alloc]init];
        infoVC.deviceModel = self.deviceModel;
        infoVC.infoModel = infoModel;
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    
}


@end
